import axios from 'axios'
import { Message } from 'element-ui'
import router from '../router/index'

axios.interceptors.request.use(config => {
    return config;
}, err => {
    Message.error({ message: '请求超时!' });
    return Promise.resolve(err);
})
axios.interceptors.response.use(data => {
    if (data.status && data.status == 200 && data.data.status == 'error') {
        Message.error({ message: data.data.msg });
        return;
    }
    return data;
}, err => {


    if (err.response.status == 504 || err.response.status == 404) {
        Message.error({ message: '服务器被吃了⊙﹏⊙∥' });
        router.push({ path: '/' });
    } else if (err.response.status == 403) {
        Message.error({ message: '权限不足,请联系管理员!' });
    } else if (err.response.status == 401) {
        window.localStorage.removeItem("user");
        router.push({ path: '/' });
    } else {
        Message.error({ message: '未知错误!' });
    }
    return Promise.resolve(err);
})
let  base = ""
// let base = 'http://39.102.76.248:9000';
const token = localStorage.getItem('token')
    // let base = 'http://localhost:8082/';
    // let base = 'http: //122.114.156.235:8889';
//console.log("base",base)
export const postRequest = (url, params) => {

    //console.log(`${base}`)
    // //console.log(`${base}${url}`,params)
    // //console.log(token)
    return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function(data) {
            let ret = ''
            for (let it in data) {

                if (data[it] instanceof Array) {

                    for (let i in data[it]) {

                        for (let property in data[it][i]) {
                            ret += it + '[' + i + '].' + property + '=' + encodeURIComponent(data[it][i][property]) + '&'
                        }

                    }
                } else {
                    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                }
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'token': token
        }
    });
}
export const uploadFileRequest = (url, params) => {
    return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        headers: {
            'Content-Type': 'multipart/form-data',
            'token': token
        }
    });
}
export const upnews = (url, params) => {
    return axios({
        method: 'get',
        url: `${base}${url}`,
        data: params
    });
}

//获取公式列表
export const gongshi = (url, params) => {
        return axios({
            method: 'post',
            url: `${base}${url}`,
            data: params
        });
    }
    //修改公式口诀
export const savegongshi = (url, params) => {
    return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function(data) {
            let ret = ''
            for (let it in data) {

                if (data[it] instanceof Array) {

                    for (let i in data[it]) {

                        for (let property in data[it][i]) {
                            ret += it + '[' + i + '].' + property + '=' + encodeURIComponent(data[it][i][property]) + '&'
                        }

                    }
                } else {
                    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                }
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'token': token
        }
    });
}
export const savenews = (url, params) => {
        return axios({
            method: 'post',
            url: `${base}${url}`,
            data: params,
            transformRequest: [function(data) {
                let ret = ''
                for (let it in data) {

                    if (data[it] instanceof Array) {

                        for (let i in data[it]) {

                            for (let property in data[it][i]) {
                                ret += it + '[' + i + '].' + property + '=' + encodeURIComponent(data[it][i][property]) + '&'
                            }

                        }
                    } else {
                        ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                    }
                }
                return ret
            }],
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'token': token
            }
        });
    }
    //删除口诀
export const delkoujue = (url) => {
    return axios({
        method: 'post',
        url: `${base}${url}`
    });
}
export const delnew = (url) => {
        return axios({
            method: 'post',
            url: `${base}${url}`
        });
    }
    //添加新的口诀公式
export const addkoujue = (url, params) => {
    return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function(data) {
            let ret = ''
            for (let it in data) {

                if (data[it] instanceof Array) {

                    for (let i in data[it]) {

                        for (let property in data[it][i]) {
                            ret += it + '[' + i + '].' + property + '=' + encodeURIComponent(data[it][i][property]) + '&'
                        }

                    }
                } else {
                    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                }
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'token': token
        }
    });
}
export const addnews = (url, params) => {
    return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function(data) {
            let ret = ''
            for (let it in data) {

                if (data[it] instanceof Array) {

                    for (let i in data[it]) {

                        for (let property in data[it][i]) {
                            ret += it + '[' + i + '].' + property + '=' + encodeURIComponent(data[it][i][property]) + '&'
                        }

                    }
                } else {
                    ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
                }
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'token': token
        }
    });
}
export const postJson = (url, params) => {
    //console.log(`${base}${url}`, params)
    const token = localStorage.getItem('token')
    //console.log('token', token)
    return axios({
        method: 'post',
        url: `${base}${url}`,
        data: params,
        headers: {
            'Content-Type': 'application/json',
            'token': token
        }
    });
}
export const putRequest = (url, params) => {
    return axios({
        method: 'put',
        url: `${base}${url}`,
        data: params,
        transformRequest: [function(data) {
            let ret = ''
            for (let it in data) {
                ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
            }
            return ret
        }],
        headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
            'token': token
        }
    });
}
export const deleteParam = (url) => {
    return axios({
        method: 'delete',
        url: `${base}${url}`,
        headers: {
            'Content-Type': 'application/json',
            'token': token
        }
    });
}
export const deleteRequest = (url) => {
    return axios({
        method: 'delete',
        url: `${base}${url}`
    });
}
export const getfenlei = (url) => {
    return axios({
        method: 'post',
        url: `${base}${url}`
    });
}
export const getRequest = (url) => {
    return axios({
        method: 'get',
        url: `${base}${url}`
    });
}